#coding: utf-8
__author__ = 'wenke1'

import time
from reportlab.graphics.charts.lineplots import LinePlot
from reportlab.graphics.charts.textlabels import Label
from reportlab.graphics.shapes import *
from reportlab.graphics import renderPDF

def displayData(*stockcode):
    lt = time.localtime()
    TIMEFORMAT = '%Y-%m-%d'
    datetime=time.strftime(TIMEFORMAT, lt)
    datafile=datetime + 'stockData.txt'

    rp=open(datafile,mode='r')
    stockdata = rp.readlines()
    #提取并格式化数据
    datadict = {}
    for i in stockcode:
        datadict[i] = [data.strip() for data in stockdata if data.split(',')[0] == i]
    rp.close()
    linesdata = []
    for code,value in datadict.items():
        stocktime = []
        stockrealprice = []
        for singledata in value:
            stime = singledata.split(',')[4].split(':')
            #hour to second
            ftime = float(stime[0])*3600 + float(stime[1])*60 + float(stime[2])
            stocktime.append(ftime)
            stockrealprice.append(float(singledata.split(',')[2]))
        code_data=zip(stocktime, stockrealprice)
        linesdata.append(code_data)

    #create a charts to display data per day
    drawing = Drawing(400, 200)

    lp = LinePlot()
    lp.x = 50
    lp.y = 50
    lp.height = 125
    lp.width = 300
    print linesdata
    lp.data = linesdata

    drawing.add(lp)
    print drawing
    renderPDF.drawToFile(drawing, 'stocktest1.pdf', 'StockData')

if __name__ == "__main__":
    stockcode=['601006','600198']
    displayData(*stockcode)